package com.zygame.sdk.view.web;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.zygame.sdk.bean.Main;
import com.zygame.sdk.bean.Navibutton;
import com.zygame.sdk.bean.Toolbar;
import com.zygame.sdk.utils.Icons;
import com.zygame.sdk.utils.ImageSelector;
import com.zygame.sdk.utils.VerifyUtil;
import com.zygame.sdk.utils.res.ResourceUtil;
import com.zygame.sdk.utils.ui.ExitAppUtils;
import com.zygame.sdk.view.BaseView;
import com.zygame.sdk.view.BottomItem;
import com.zygame.sdk.view.JSplugin;

//@SuppressLint("JavascriptInterface")
public class FYToolBarWebView extends BaseView {
	private MyWebView fy_toolbar_wb_refined;
	private ProgressBar fy_toolbar_pb_refined;

	private Button btn_nav_top_left, btn_nav_top_right;
	private RadioGroup ll_top_middle;

	public FYToolBarWebView(Context context) {
		super(context);
		this.context = context;
		// TODO Auto-generated constructor stub
	}

	public FYToolBarWebView(Context context, String url) {
		super(context);
		
		fy_toolbar_wb_refined.loadUrl(initUrl(url));
//		System.out.println("url = " + initUrl(url));
	}

	public FYToolBarWebView(Context context, Toolbar toolbar) {
		super(context);
		
//		System.out.println("url = " + initUrl(toolbar.url));
		fy_toolbar_wb_refined.loadUrl(initUrl(toolbar.url));
		initTopButtons(toolbar);
	}

	public FYToolBarWebView(Context context, Main main) {   
		super(context);
		fy_toolbar_wb_refined.loadUrl(initUrl(main.url));
		initTopButtons4Main(main);
	}

	public String initUrl(String url) {
		return VerifyUtil.installUrl(context, url);
	}

	@SuppressLint("NewApi")
	private void initTopButtons(Toolbar toolbar) {
		// TODO Auto-generated method stub
		int count = 0;
		int count1 = 0;

		ArrayList<Navibutton> navibuttons = (ArrayList<Navibutton>) toolbar.navibuttons;
		for (Navibutton navibutton : navibuttons) {
			if (navibutton == null)
				continue;
			if (navibutton.pos.equals("1")) {
				btn_nav_top_left.setText(navibutton.name);
				// btn_nav_top_left.setBackground(ImageSelector.newSelector(context,Icons.getInstance().getIcons(navibutton.src,
				// 0), Icons.getInstance().getIcons(navibutton.src, 1)));
			} else if (navibutton.pos.equals("2")) {
				RadioButton rb = new BottomItem(context, navibutton);
				rb.setTag(navibutton);
				rb.setOnClickListener(this);
				ll_top_middle.addView(rb);
				count1++;
			} else if (navibutton.pos.equals("3")) {
				btn_nav_top_right.setVisibility(View.VISIBLE);
				btn_nav_top_right.setText(navibutton.name);
				btn_nav_top_right.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						// goBack2();
						// ((Activity) context).finish();
						ExitAppUtils.getInstance().exit();
					}
				});
				// btn_nav_top_right.setBackground(ImageSelector.newSelector(context,Icons.getInstance().getIcons(navibutton.src,
				// 0), Icons.getInstance().getIcons(navibutton.src, 1)));
				count++;
			}
		}
		if (ll_top_middle.getChildAt(0) != null) {
			ll_top_middle.getChildAt(0).performClick();
		}
		btn_nav_top_right.setVisibility(count == 0 ? View.GONE : View.VISIBLE);
		if (count1 == 0) {
			TextView textView = new TextView(context);
			textView.setText(toolbar.name);
			textView.setTextColor(Color.parseColor("#FFFFFF"));
			ll_top_middle.addView(textView);
		}
	}

	@SuppressLint("NewApi")
	private void initTopButtons4Main(Main main) {
		// TODO Auto-generated method stub
		int count = 0;
		int count1 = 0;
		ArrayList<Navibutton> navibuttons = (ArrayList<Navibutton>) main.navibuttons;
		// if(main.name.equals("客服")){
		//
		// }else{
		for (Navibutton navibutton : navibuttons) {
			if (navibutton == null)
				continue;
			if (navibutton.pos.equals("1")) {
				btn_nav_top_left.setText(navibutton.name);
				// btn_nav_top_left.setBackground(ImageSelector.newSelector(
				// context, Icons.getInstance()
				// .getIcons(navibutton.src, 0), Icons
				// .getInstance().getIcons(navibutton.src, 1)));
			} else if (navibutton.pos.equals("2")) {
				RadioButton rb = new BottomItem(context, navibutton);
				rb.setTag(navibutton);
				rb.setOnClickListener(this);
				ll_top_middle.addView(rb);
				count1++;
			} else if (navibutton.pos.equals("3")) {
				btn_nav_top_right.setVisibility(View.VISIBLE);
				btn_nav_top_right.setText(navibutton.name);
				btn_nav_top_right.setBackgroundDrawable(ImageSelector.newSelector(context, Icons.getInstance().getIcons(context, navibutton.src, 0), Icons.getInstance().getIcons(context, navibutton.src, 1)));
				count++;
			}
		}
		// }

		if (ll_top_middle.getChildAt(0) != null) {
			ll_top_middle.getChildAt(0).performClick();
		}
		btn_nav_top_right.setVisibility(count == 0 ? View.GONE : View.VISIBLE);
		if (count1 == 0) {
			TextView textView = new TextView(context);
			textView.setTextColor(Color.parseColor("#FFFFFF"));
			textView.setText(main.name);
			ll_top_middle.addView(textView);
		}
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		Navibutton navibutton = (Navibutton) v.getTag();
		if (navibutton == null) {
			if (v.getId() == ResourceUtil.getId(context, "btn_nav_top_left")) {
				goBack2();
			}
		} else {
			fy_toolbar_wb_refined.loadUrl(navibutton.url);
		}

	}

	@Override
	protected void init() {
		// TODO Auto-generated method stub
		showView = (ViewGroup) View.inflate(context, ResourceUtil.getLayoutId(context, "fy_view_toolbar_refined"), null);
		findView(showView);

		fy_toolbar_wb_refined = (MyWebView) findViewByName("fy_toolbar_wb_refined");
		fy_toolbar_pb_refined = (ProgressBar) findViewByName("fy_toolbar_pb_refined");

		fy_toolbar_wb_refined.addJavascriptInterface(new JSplugin((Activity) context), "WebViewJs");

		fy_toolbar_wb_refined.setWebViewClient(new WebViewClient() {
			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				// TODO Auto-generated method stub
				return false;
			}

			@Override
			public void onPageFinished(WebView view, String url) {
				// TODO Auto-generated method stub
				super.onPageFinished(view, url);
				fy_toolbar_pb_refined.setVisibility(View.GONE);
			}
		});
	}

	private void findView(ViewGroup showView) {
		// TODO Auto-generated method stub
		btn_nav_top_left = (Button) showView.findViewById(ResourceUtil.getId(context, "btn_nav_top_left"));
		btn_nav_top_right = (Button) showView.findViewById(ResourceUtil.getId(context, "btn_nav_top_right"));
		ll_top_middle = (RadioGroup) showView.findViewById(ResourceUtil.getId(context, "ll_top_middle"));
		setListener(new View[] { btn_nav_top_left, btn_nav_top_right });
	}

	public boolean goBack() {
		if (fy_toolbar_wb_refined != null && fy_toolbar_wb_refined.canGoBack()) {
			fy_toolbar_wb_refined.goBack();
			return true;
		}
		return false;
	}

	public void goBack2() {
		if (fy_toolbar_wb_refined != null && fy_toolbar_wb_refined.canGoBack()) {
			fy_toolbar_wb_refined.goBack();
		} else {
			((Activity) context).finish();
		}
	}

	@Override
	public int getId() {
		// TODO Auto-generated method stub
		return 0;
	}

	// /**
	// * @author K
	// * @Description: TODO(js修改密码后保存)
	// * @date 2014-12-8 下午2:58:58
	// * @param passWord 密码
	// */
	// public void jsChangePassWordEvent(String passWord) {
	// SharedPreferencesHelper helper = SharedPreferencesHelper.getInstance();
	// helper.savePassWord(context, passWord);
	// }

}
